<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Đặng Khánh Trung
 * Date: 11/9/12
 * Time: 1:04 PM
 * To change this template use File | Settings | File Templates.
 */
defined('_JEXEC') or die;

class VastViewDonvitheokhois extends JViewLegacy
{
    protected $items;
    protected $pagination;
    protected $state;
    protected $canDo;

    /**
     * Display the view
     *
     * @return    void
     */
    public function display($tpl = null)
    {
        $session = JFactory::getSession();
        
        $id = JRequest::getInt('khoi_id', 0);
        
        if ($id !== 0)
        {
            $session->set('donvitheokhois.khoi_id', $id);
        }

        $this->items = $this->get('Items');
        $this->pagination = $this->get('Pagination');
        $this->state = $this->get('State');

        JLoader::register('DonvisHelper', JPATH_COMPONENT . '/helpers/donvi.php');

        $this->canDo = DonvisHelper::getActions();

        // Check for errors.
        if (count($errors = $this->get('Errors'))) {
            JError::raiseError(500, implode("\n", $errors));
            return false;
        }

        $this->addToolbar();

        parent::display($tpl);
    }

    /**
     * Add the page title and toolbar.
     *
     * @since    1.6
     */
    protected function addToolbar()
    {
        $session = JFactory::getSession();
        
        $id = (int)$session->get('donvitheokhois.khoi_id');        
        
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);

        $query->select('a.*');
        $query->from('#__khoi AS a');
        $query->where('a.id = ' . $id);

        $db->setQuery($query);
        $column = $db->loadObjectList();
                        
        if (count($column) > 0)
        {
            JToolBarHelper::title(JText::_('COM_VAST_MANAGER_DONVITHEOKHOIS') . ' ' . $column[0]->ten, 'head vm_manufacturer_48');
        }
        else
            JToolBarHelper::title(JText::_('COM_VAST_MANAGER_DONVITHEOKHOIS'), 'head vm_manufacturer_48');

        if ($this->canDo->get('vast.khoi.add.donvi')) {
            JToolBarHelper::custom('donvitheokhoi.addCustom', 'new', 'new', 'Thêm đơn vị vào khối', false);        
        }
        
        if ($this->canDo->get('vast.donvi.quanlyphong')) {
            JToolBarHelper::custom('phongtheodonvi.show2phong', 'checkin', 'checkin', 'COM_VAST_JTOOLBAR_PHONGTHEODONVI');
            JToolBarHelper::divider();            
        }
        
        if ($this->canDo->get('vast.donvi.edit')) {
            JToolBarHelper::editList('donvi.edit', 'COM_VAST_DONVI_JTOOLBAR_EDIT');
        }
        if ($this->canDo->get('vast.donvi.delete')) {
            JToolBarHelper::deleteList('', 'donvitheokhois.delete', 'COM_VAST_DONVI_JTOOLBAR_DELETE');
        }
        if ($this->canDo->get('core.admin')) {
            JToolBarHelper::divider();
            JToolBarHelper::preferences('com_vast', '550', '875', 'COM_VAST_DONVI_JTOOLBAR_OPTIONS');
        }
    }
}